import { reqSiteApi, reqBoxListApi } from '@/api/siteApi'
import { OK_CODE } from '@/app/keys'

const useSiteConfig = () => {
  const loading = ref(true)
  const site_config = ref({})
  const search_config = ref({})

  const load = async () => {
    loading.value = true
    try {
      const { code, data } = await reqSiteApi()
      if (code === OK_CODE) {
        site_config.value = data.site_config
        search_config.value = data.search_config
      }
      loading.value = false
    } catch (error) {
      loading.value = false
    }
  }

  load()

  return {
    loading,
    site_config,
    search_config,
    load,
  }
}

export const useBoxList = (id) => {
  const loading = ref(false)
  const box_list = ref([])
  const refresh = async () => {
    loading.value = true
    try {
      const { data, code } = await reqBoxListApi(id.value)
      if (code === OK_CODE) {
        box_list.value = data
      }
      loading.value = false
    } catch (error) {
      loading.value = false
    }
  }
  watch(id, async () => {
    await refresh()
  })
  return {
    loading,
    box_list,
    refresh,
  }
}

export default useSiteConfig

